import mysql from "mysql";

const conn = mysql.createConnection({
    host: "127.0.0.1",
    port: 3306,
    user: "root",
    password: "",
    database: "yijianet"
});


function exec(sql, params, callback) {

    conn.connect(function (err) {
        if (err) {
            console.log("链接失败:" + err.message);
        
        }
    });
    conn.query(sql, params, (err, result, fields) => {
        callback(err, result, fields);
    })

}

function execAsync(sql, params) {

    return new Promise((resolve, reject) => {
        conn.connect(function (err) {
            if (err) {
                console.log("链接失败:" + err.message);
             return;
            }
        });

        conn.query(sql, params, (err, result, fields) => {
            if (err) {
                reject(err, result, fields);
                return;
            }
            resolve(result, fields);
        })
    })

}



module.exports = {
    exec,
    execAsync
}

